<template>
  <div class="login">
    <div class="form">
      <img src="../../assets/common/login-logo.png" alt="" class="img">
      <el-form ref="form" :rules="rules" :model="form">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          // 自定义手机号码验证
          {
            validator: (rule, value, callback) => {
              const res =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (res.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确的手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 6,
            message: '请输入6位验证码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await this.$store.dispatch('user/toLogin', this.form)

          this.$message.success('验证通过')
          // 路由回跳,没有就去首页
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    width: 520px;
    height: 460px;
    .el-input {
      // height: 75px;
      margin: 10px 0;
    }
    .el-button {
      width: 100%;
    }
    .img {
      width: 100%;
      height: 50px;
    }
  }
}
</style>
